function GM_xmlhttpRequest(details) {
    if (!window.electronAPI || typeof window.electronAPI.gmXmlHttpRequest !== 'function') {
        throw new Error("window.electronAPI.gmXmlHttpRequest 未定义，确保 preload.js 中已暴露接口");
    }

    // 默认参数
    const {
        method = 'GET',
        url,
        headers = {},
        data = null,
        timeout = 30000,
        responseType = 'text',
        onload = () => {},
        onerror = () => {},
        ontimeout = () => {}
    } = details;

    let isAborted = false;

    const timer = setTimeout(() => {
        isAborted = true;
        ontimeout({
            status: 0,
            statusText: 'Request timed out',
        });
    }, timeout);

    window.electronAPI.gmXmlHttpRequest({
        method,
        url,
        headers,
        data,
        responseType,
        timeout
    }).then(response => {
        clearTimeout(timer);
        if (isAborted) return;

        if (response.error) {
            onerror(response);
        } else {
            onload(response);
        }
    }).catch(error => {
        clearTimeout(timer);
        if (isAborted) return;

        onerror({
            status: 0,
            statusText: error.message || "Unknown error",
            error
        });
    });

    return {
        abort: () => {
            clearTimeout(timer);
            isAborted = true;
        }
    };
}